package edu.zut.hys.chatpageapplication.provider;

import edu.zut.hys.chatpageapplication.ResourceTable;
import edu.zut.hys.chatpageapplication.bean.AMessage;
import ohos.aafwk.ability.DataAbilityHelper;
import ohos.aafwk.ability.DataAbilityRemoteException;
import ohos.agp.components.*;
import ohos.agp.window.dialog.CommonDialog;
import ohos.app.Context;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.utils.net.Uri;

import java.io.FileDescriptor;
import java.io.FileNotFoundException;
import java.util.List;

public class MessageItemProvider extends BaseItemProvider {

    private List<AMessage> messageData;

    private final Context context;

    public MessageItemProvider(Context context, List<AMessage> messageData) {
        this.messageData = messageData;
        this.context = context;
    }

    @Override
    public int getCount() {
        return messageData.size();
    }

    @Override
    public Object getItem(int position) {
        return messageData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
        return getItemComponent(position);
    }

    private Component getItemComponent(int position) {
        return getComponent(position);
    }

    private Component getComponent(int position) {
        AMessage aMessage = messageData.get(position);
        if(aMessage.getType().equals("string")){
            Component container = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_message_list_item, null, false);
            Text message = (Text) container.findComponentById(ResourceTable.Id_message);
            message.setText(aMessage.getMessage());
            return container;
        }else if(aMessage.getType().equals("image")){
            Component container = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_image_message_list_item, null, false);
            Image image = (Image) container.findComponentById(ResourceTable.Id_image_message);

            DataAbilityHelper helper=DataAbilityHelper.creator(context);
            ImageSource imageSource;
            Uri uri = Uri.parse(aMessage.getMessage());
            FileDescriptor fd = null;
            try {
                fd = helper.openFile(uri, "r");
            } catch (DataAbilityRemoteException | FileNotFoundException e) {
                e.printStackTrace();
            }
            imageSource = ImageSource.create(fd, null);
            //创建位图
            PixelMap pixelMap = imageSource.createPixelmap(null);
            image.setPixelMap(pixelMap);
            imageSource.release();
            helper.release();
            return container;
        }
        Component container = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_message_list_item, null, false);
        Text message = (Text) container.findComponentById(ResourceTable.Id_message);
        message.setText(aMessage.getMessage());
        return container;
    }


    /**
     * update list data
     *
     * @param data contactDevicesData list
     */
    public void update(List<AMessage> data) {
        messageData = data;
        notifyDataChanged();
    }

}
